<?php

class Pilotage_ProjectbordController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        // action body
    	$this->_helper->layout()->disableLayout();
    	$ListDesProject=new Application_Model_Mapper_PilotProject();

    	$projectList=array();
    	$loaderFile=$ListDesProject->LoadProjectForRequestWs();
    	foreach($loaderFile as $key=>$value){
    		$projectList[$value['idproject']]['name']=$value['nomprojet'];
    		$projectList[$value['idproject']]['clients'][$value['idmodelanalyse']]['nom']=$value['nomclient'];
    		$projectList[$value['idproject']]['clients'][$value['idmodelanalyse']]['url']=$value['url'];
    		$projectList[$value['idproject']]['clients'][$value['idmodelanalyse']]['idanalyse']=$value['idPILOT_PROJETANALYSE'];
    		$projectList[$value['idproject']]['clients'][$value['idmodelanalyse']]['idmodel']=$value['idmodelanalyse'];
    		$projectList[$value['idproject']]['clients'][$value['idmodelanalyse']]['service']=$value['servicedemande'];
    		$projectList[$value['idproject']]['clients'][$value['idmodelanalyse']]['idclient']=$value['idclient'];
    		$projectList[$value['idproject']]['clients'][$value['idmodelanalyse']]['nommodel']=$value['nommodelanalyse'];
    	}
    	
    	$this->view->project=$projectList;
    	
    }

    public function loaddataboardAction()
    {
        // action body
    	$id =$this->_request->getPost();  
    	$this->_helper->layout()->disableLayout();
    	$this->_helper->viewRenderer->setNoRender(true);
    	
        $pilot_jsondata = new Application_Model_Mapper_PilotDatajson();
        $selector= $pilot_jsondata->DataJsonAddDate($id['id'],$id['idplan']);

        echo $selector['data'];
    }

    public function jsonstructerdataboardAction()
    {
        // action body
        
    	$this->_helper->layout()->disableLayout();
    	$this->_helper->viewRenderer->setNoRender(true);
    	
    	//$idmodel=$this->getRequest()->getParam("id");
    	    	
    	$mapperducolm=new Application_Model_Mapper_PilotRequest();
    	$idmodel=$this->_request->getPost();
    	
    	
    	
    	$datacolumn=$mapperducolm->extractRequest("",$idmodel['id']);
    	$columsheader="";
    	foreach ($datacolumn as $key => $value){
    		$columsheader.=empty($columsheader) ? "'".$value['LABELCOLUMN']."'" : ",'".$value['LABELCOLUMN']."'" ;
    	}
    	$columsheader.=",'DatePlanLoad'";
    	echo "[".$columsheader."]";
    		
    }

    public function projectsactionsAction()
    {
        // action body
    	$this->_helper->viewRenderer->setNoRender(true);
    	$project =new Application_Model_Mapper_PilotListclients();
    	
    	$dataclient=$project->fetchAllToArray();
    	$table=array();
    	foreach ($dataclient as $key => $value){
    		$table[]=array(
    						'nom'=>$value['NOMCLIENT'],
    						'idclient'=>$value['idPILOT_LISTCLIENTS']
    				);
    	}
    
    	echo Zend_Json::encode($table);
    	
    }

    public function assigneactivitemodelAction()
    {
        // action body
    	
        
    }

    public function dycolumnboardAction()
    {
        // action body
        
    	$this->_helper->layout()->disableLayout();
    	$this->_helper->viewRenderer->setNoRender(true);
    	
    	$mapperducolm=new Application_Model_Mapper_PilotRequest();
    	
    	
    	$idmodel=$this->_request->getPost();
    	$datacolumn=$mapperducolm->extractRequest("",$idmodel['id']);
    	//$colums="";
    	$i=0;
    	foreach ($datacolumn as $key => $value){
    		/*if(!empty($colums)){
    			$colums.=",";
    		}*/
    		$colum['header']=$value['LABELLANGUE'];
    		$colum['dataIndex']=$value['LABELCOLUMN'];
    		//$colum['flex']="3";
    		//$colum['width']="100;
    		//$colums.="{ header: '".$value['LABELLANGUE']."',  dataIndex: '".$value['LABELCOLUMN']."',  flex: 1 }";
    		$colums[]=$colum;
    		$i++;
    	}
    	$colum['header']="Date chargement";
    	$colum['dataIndex']="DatePlanLoad";
       // $colum['flex']="3";
    //	$colum['width']=auto;
    	
    	$colums[]=$colum;
    	echo Zend_Json::encode($colums);
    	
     }
     
     public function projectdatasAction(){
     	$this->_helper->layout()->disableLayout();
     	$this->_helper->viewRenderer->setNoRender(true);
     	
     	$datapostednode=$this->_request->getPost ();
     	
     	
     	$datanodes=array();
     	
     	if( isset($datapostednode["node"])){
     	
     		$datanodes=explode("_", $datapostednode["node"]);
     	
     	}
     	
     	$noeuds=array();
     	
     	if(isset($datanodes[0]) && $datanodes[0]=="PROJECT" ){
     		
     		
     		$projects=new Application_Model_Mapper_PilotProject();
     		
     		
     		/*$droitsurprojet=new Application_Model_Mapper_PilotProjetRol();
			$droit=$droitsurprojet->getMesProject(Zend_Auth::getInstance()->getIdentity()->idpersonnes);
     		
			if(!empty($droit)){
				$prohectsdatas=$projects->LoadProjectForRequestWs($datanodes[1],$droit);
			}else{
				$prohectsdatas=$projects->LoadProjectForRequestWs($datanodes[1]);
			}*/
     		$prohectsdatas=$projects->LoadProjectForRequestWs($datanodes[1]);
     		
     		 
     		
     		
     		
     		
     		foreach ($prohectsdatas as $value){
     			$noeud['text']=$value['NOMMODEL'];
     			$noeud['expanded']="false";
     			$noeud['MyName']=$value["idPILOT_MODELANALYSE"];
     			$noeud['id']="MODEL_".$value["idPILOT_MODELANALYSE"];
     			$noeuds[]=$noeud;
     			
     			
     			$noeud2['text']=$value["URLPLATEFORME"];
     			$noeud2['expanded']="false";
     			$noeud2['leaf']="true";
     			$noeud2['MyName']=$value["idPILOT_MODELANALYSE"];
     			$noeud2['id']="PL_".$value['PILOT_PLATEFORME_idPILOT_PLATEFORME'];
     			$noeuds[]=$noeud2;
     		}
     		
     	}elseif(isset($datanodes[0]) && $datanodes[0]=="MODEL"){
     		
     		
     		$datapostednode=$this->_request->getPost ();
     		
     		$planing=new Application_Model_Mapper_PilotLoadplan();
     		$listPlan=$planing->getPlanOfModel($datanodes[1]) ;
     	//	print_r($listPlan);
     		foreach ($listPlan as $value){
     			$noeud['text']=$value['dateloading'];
     			$noeud['expanded']="false";
     			$noeud['MyName']=$value["idPILOT_MODELANALYSE"];
     			$noeud['leaf']="true";
     			$noeud['id']="LOT_".$value["PILOT_PROJETANALYSE_idPILOT_PROJETANALYSE"]."_".$value["idPILOT_LOADPLAN"]."_".$value["idPILOT_MODELANALYSE"];
     			$noeuds[]=$noeud;

     		}
     		
     	}    	
     	else{
     		
	     	$projects=new Application_Model_DbTable_PilotProject();
	     	
	     	$droitsurprojet=new Application_Model_Mapper_PilotProjetRol();
	     	$droit=$droitsurprojet->getMesProject(Zend_Auth::getInstance()->getIdentity()->idpersonnes);
	     	 
	     	if(!empty($droit)){
	     		$prohectsdatas=$projects->fetchAll(" pilot_project.idPILOT_PROJECT IN ($droit)")->toArray();
	     	}else{
	     		$prohectsdatas=$projects->fetchAll()->toArray();
	     	} 
	     	
	     	
	     	
	     	foreach ($prohectsdatas as $value){
	     		$noeud['text']=$value['NomProjet'];
	     		$noeud['MyName']=$value["NomProjet"];
	     		$noeud['expanded']="false";
	     		$noeud['id']="PROJECT_".$value["idPILOT_PROJECT"];
	     		$noeuds[]=$noeud;
	     	}
     	}
     	echo Zend_Json::encode($noeuds);
     	
     }

     
     public function piedashbordAction(){
     	
     	$this->_helper->layout()->disableLayout();
     	$this->_helper->viewRenderer->setNoRender(true);
     	
     	$datapostednode=$this->_request->getPost ();
     	
     	
     	$mapperrequest=new Application_Model_Mapper_PilotRequest();
     	$resdefcol=$mapperrequest->extractRequest("",$datapostednode['id']);
     	
     	
     	if(isset($resdefcol[0]['WITHCHART']) && $resdefcol[0]['WITHCHART']=="NON"){
     		echo "NONE";
     		return;
     	}
     	
     	$tabres=array();
     	
     	foreach($resdefcol as $value){
     		if($value['ISCHART']=='1'){
     			if($value['CHART_AXE']=="name"){
     				$tabres['name']=$value['LABELCOLUMN'];
     			}elseif($value['CHART_AXE']=="x"){
     				$tabres['x']=$value['LABELCOLUMN'];
     			}
     		}
     	}
     	
     	
     	
     	$tab['type']='pie';
     	$tab['field']=$tabres['x'];
     	$tab['showInLegend']= false;
     	$tab['donut']=true;
     	$tab['highlight']['segment']['margin']=20;
     	$tab['label']=array(
     		'field'=>$tabres['name'],
     		'display'=> 'rotate',
     		'contrast'=>true,
     		'font'=> '18px Arial'
     	);
     	
     	
     	echo Zend_Json::encode($tab);
     }

}
